package org.onion.pattern.kit.validation.annotation.validator;


import org.onion.pattern.kit.validation.annotation.IsBizCode;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import org.apache.commons.lang3.StringUtils;

import java.util.regex.Pattern;

/**
 * 字母，数值，下划线，或者号
 *
 */
public class IsBizCodeValidator implements ConstraintValidator<IsBizCode, String> {

    private final static Pattern PATTERN = Pattern.compile("^[a-zA-Z0-9_-]*$");

    @Override
    public void initialize(IsBizCode constraintAnnotation) {

    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (StringUtils.isBlank(value)) {
            return true;
        }

        return PATTERN.matcher(value).find();
    }

}
